
<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column show-overflow-tooltip align="center" v-for="item in columns" :key="item.prop" :prop="item.prop" :label="item.label">
              <template slot-scope="scope">
                <el-image v-if="item.type === 'image'" :preview-src-list="(scope.row[item.prop] || '').split(',')" :src="scope.row[item.prop]" style="width: 30px;height: 30px;border: 1px solid #3CAA49" fit="contain" />
                <span v-else-if="item.type === 'select'">
                  {{ item.mapping[scope.row[item.prop]] }}
                </span>
                <span v-else>{{ scope.row[item.prop] }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="150px">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="page-box">
          <el-pagination
              background
              :current-page="pageNo"
              :page-sizes="pageSizes"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalSize"
              @size-change="changePageSize"
              @current-change="changePageNo"
          />
        </div>
      </el-card>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
export default {
  name: "index",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.product,
      searchList: [
        {
          label: '名称',
          prop: 'name',
          name: 'input',
          clearable: true
        },
        {
          label: '所属分类',
          prop: 'category_id',
          name: 'select',
          options: [],
          clearable: true
        },
        {
          label: '所属礼包',
          prop: 'pid',
          name: 'select',
          options: [],
          clearable: true
        },
      ],
      searchParams: {
        name: ''
      },
      columns: [
        { prop: 'categoryName', label: '所属分类' },
        { prop: 'giftName', label: '所属礼包' },
        { prop: 'name', label: '商品名称' },
        { prop: 'code', label: '商品编号' },
        { prop: 'post_url', label: '封面', type: 'image' },
        { prop: 'sort', label: '排序' },
        { prop: 'status', label: '启用状态', type: 'select', mapping: { 0: '禁用', 1: '启用'} },
        { prop: 'create_time', label: '创建时间' },
      ],
      tableData: [

      ]
    }
  },
  created() {
    http.category.list().then(res => {
      this.searchList[1].options = res.data.map(item => {
        return {
          label: item.name,
          value: item.id
        }
      })
    })
    http.product.list({
      id: 0
    }).then(res => {
      this.searchList[2].options = res.data.map(item => {
        return {
          label: item.name,
          value: item.id
        }
      })
    })
  },
  methods: {
  }
}
</script>

<style scoped lang="scss">

</style>
